package com.xlh.service.system.impl;

import com.xlh.dao.system.ResourceRelationMapper;
import com.xlh.dao.system.ext.ResourceRelationMapperExt;
import com.xlh.dao.system.ResourceMapper;
import com.xlh.dao.system.ext.ResourceMapperExt;
import com.xlh.dto.system.ResourceRelationDTO;
import com.xlh.param.course.ResourcesParam;
import com.xlh.pojo.system.ResourceRelation;
import com.xlh.pojo.system.ResourceRelationExample;
import com.xlh.pojo.system.Resource;
import com.xlh.pojo.system.ResourceExample;
import com.xlh.service.system.SystemResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 模版
 * <p>
 * Created by lx on 2019/3/10 0010.
 */
@Service
public class SystemResourceServiceImpl implements SystemResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private ResourceMapperExt resourceMapperExt;

    @Autowired
    private ResourceRelationMapper resourceRelationMapper;

    @Autowired
    private ResourceRelationMapperExt resourceRelationMapperExt;

    @Override
    public List<Resource> queryResource(List<Long> ids) {
        ResourceExample example = new ResourceExample();
        example.createCriteria().andIdIn(ids);
        return resourceMapper.selectByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertResource(Resource resource) {
        resourceMapper.insertSelective(resource);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateResource(Resource resource) {
        resourceMapper.updateByPrimaryKeySelective(resource);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<Long> ids) {
        ResourceExample resourceExample = new ResourceExample();
        resourceExample.createCriteria().andIdIn(ids);
        resourceMapper.deleteByExample(resourceExample);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<Resource> resources) {
        resourceMapperExt.insertBatch(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBatch(List<Resource> resources) {
        resourceMapperExt.updateBatch(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBatchResourceRelation(List<ResourceRelation> resourceRelations) {
        resourceRelationMapperExt.insertBatch(resourceRelations);
    }

    @Override
    public List<ResourceRelationDTO> getResourceRelationInfo(
            List<Long> objIds, List<Integer> types, List<String> names, Integer type) {
        return resourceRelationMapperExt.getResourceRelation(objIds, types, names, type);
    }

    @Override
    public List<ResourceRelationDTO> queryResourceRelation(List<ResourcesParam> data) {
        return resourceRelationMapperExt.queryResourceRelation(data);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteResourceRelation(List<Long> resourceRelationIds) {
        ResourceRelationExample example = new ResourceRelationExample();
        example.createCriteria().andIdIn(resourceRelationIds);
        resourceRelationMapper.deleteByExample(example);
    }

    @Override
    public List<ResourceRelation> queryResourceRelation(ResourceRelationExample example) {
        return resourceRelationMapper.selectByExample(example);
    }

    @Override
    public List<Resource> queryResource(ResourceExample example) {
        return resourceMapper.selectByExample(example);
    }
}
